//
//  PCMeStore.m
//  PhotoCircle
//
//  Created by GL on 15/12/15.
//  Copyright © 2015年 wujungao. All rights reserved.
//

#import "PCMeStore.h"
#import "PCUserModel.h"
#import "GMHTTPSessionManager.h"
#import "PCUserInfoManager.h"
#import "PCUserAlbumModel.h"
#import "PCUserGameModel.h"
#import "uploadJoinPhotoMatchModel.h"

static NSString *userIndex_Path         = @"/api/v1/my/index.json";
static NSString *userCaptcha_Path       = @"/api/v1/user/send_captcha.json";
static NSString *userRegister_Path      = @"/api/v1/user/register.json";
static NSString *userLogin_path         = @"/api/v1/user/login.json";
static NSString *userResetPassWord_Path = @"/api/v1/user/reset_pwd.json";
static NSString *userUpDateUserInfo_Path = @"/api/v1/my/setting/update_attr.json";

static NSString *userAlbum_Path = @"/api/v1/my/album/index.json";
static NSString *userGame_Path  = @"/api/v1/my/game/index.json";
static NSString *userFeedBakc_Back = @"/api/v1/my/feedback/create.json";

static NSString *userModifyHeader_Path = @"/api/v1/my/setting/upload_logo.json";
static NSString *aliPay_path = @"/api/v1/my/payment/alipay_pay.json";
static NSString *userGiveUpGame_path = @"/api/v1/my/game/quit.json";
static NSString *upLoadRealImage_Path = @"/api/v1/my/game/upload_real_photo.json";
static NSString *upLoadPhotoToAlbum_Path=@"/api/v1/my/album/upload.json";
static NSString *upLoadPhotoForJoinMatch_Path=@"/api/v1/game/participate.json";

@implementation PCMeStore

#pragma mark -
#pragma mark - Singleton Methods

+ (instancetype)sharedMeStore
{
    static PCMeStore *store = nil;
    static dispatch_once_t once = 0;
    dispatch_once(&once, ^{
        store = [[PCMeStore alloc] init];
    });
    return store;
}

-(PCUserGameModel *)mGameModel
{
    if (!_mGameModel) {
        _mGameModel = [[PCUserGameModel alloc] init];
    }
    return _mGameModel;
}

-(void)clearUserInfo{
    self.userModel = nil;
    self.mGameModel = nil;
}

-(void)selectedIndexGameModel:(PCUserGameModel *)gModel
{
    self.mGameModel = gModel;
}

// 请求个人中心数据
-(void)requestUserListDataWith:(NSDictionary*)paramDic
                successHandler:(successBlock)responSuccess
                  errorHandler:(failBlock)responError
{
    NSString *requestUrl = SERVER_URL(userIndex_Path);
    [[GMHTTPSessionManager shared] GET:requestUrl parameters:paramDic success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        
        NSUInteger responseCode = [responseObject[@"code"] integerValue];
        NSLog(@"responseObject :%@",responseObject);
        if (responseCode == API_RESPONSE_OK) {
            PCUserModel *uModel = [[PCUserModel alloc] initWithDictionary:responseObject[@"data"]];
            self.userModel = uModel;
            // 存储新的用户信息
            [[PCUserInfoManager sharedUserInfo] cleanLocalUserInfo];
            [[PCUserInfoManager sharedUserInfo] saveUserInfo:uModel];
        }
        responSuccess(responseCode);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"error:%@",error);
        responError(error);
    }];
}

// 请求验证码
-(void)requestUserCaptcha:(NSDictionary*)paramDic
           successHandler:(successBlock)responSuccess
             errorHandler:(failBlock)responError
{
    NSString *requestUrl = SERVER_URL(userCaptcha_Path);
    
    [[GMHTTPSessionManager shared] POST:requestUrl parameters:paramDic success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSUInteger responseCode = [responseObject[@"code"] integerValue];
        NSLog(@"responseObject :%@",responseObject);
        responSuccess(responseCode);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        responError(error);
    }];
    
}

// 用户注册
-(void)requestUserRegister:(NSDictionary*)paramDic
            successHandler:(successBlock)responSuccess
              errorHandler:(failBlock)responError
{
    NSString *requestUrl = SERVER_URL(userRegister_Path);
    
    [[GMHTTPSessionManager shared] POST:requestUrl parameters:paramDic success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSUInteger responseCode = [responseObject[@"code"] integerValue];
        NSLog(@"responseObject :%@",responseObject);
        NSLog(@"data :%@",responseObject[@"data"]);
        //NSLog(@"responseCode string:%ld",(long)[responseObject[@"code"] integerValue]);
        // -10105号码已注册
        if (responseCode == API_RESPONSE_OK) {
            PCUserModel *uModel = [[PCUserModel alloc] initWithDictionary:responseObject[@"data"]];
            self.userModel = uModel;
        }
        responSuccess(responseCode);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        responError(error);
    }];
}

// 用户登录
-(void)requestUserLogin:(NSDictionary*)paramDic
         successHandler:(successBlock)responSuccess
           errorHandler:(failBlock)responError
{
    NSString *requestUrl = SERVER_URL(userLogin_path);
    
    [[GMHTTPSessionManager shared] POST:requestUrl parameters:paramDic success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSUInteger responseCode = [responseObject[@"code"] integerValue];
        NSLog(@"responseObject :%@",responseObject);
        if (responseCode == API_RESPONSE_OK) {
            PCUserModel *uModel = [[PCUserModel alloc] initWithDictionary:responseObject[@"data"]];
            self.userModel = uModel;
        }
        responSuccess(responseCode);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        responError(error);
    }];
}

// 修改密码
-(void)requestResetPassWord:(NSDictionary*)paramDic
             successHandler:(successBlock)responSuccess
               errorHandler:(failBlock)responError{
    
    NSString *requestUrl = SERVER_URL(userResetPassWord_Path);
    
    [[GMHTTPSessionManager shared] POST:requestUrl parameters:paramDic success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSUInteger responseCode = [responseObject[@"code"] integerValue];
        NSLog(@"responseObject :%@",responseObject);
        if (responseCode == API_RESPONSE_OK) {
            PCUserModel *uModel = [[PCUserModel alloc] initWithDictionary:responseObject[@"data"]];
            self.userModel = uModel;
        }
        responSuccess(responseCode);

    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        responError(error);
    }];
}

#pragma mark -
#pragma mark -修改用户信息
//修改昵称
-(void)modifyUserName:(NSDictionary*)paramDic
       successHandler:(successBlock)responSuccess
         errorHandler:(failBlock)responError
{
    NSString *requestUrl = SERVER_URL(userUpDateUserInfo_Path);
    [[GMHTTPSessionManager shared] PUT:requestUrl parameters:paramDic success:^(NSURLSessionDataTask *task, id responseObject) {
        NSUInteger responseCode = [responseObject[@"code"] integerValue];
        //NSLog(@"responseObject :%@",responseObject);
        responSuccess(responseCode);

    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}

//修改性别
-(void)modifyUserGender:(NSDictionary*)paramDic
         successHandler:(successBlock)responSuccess
           errorHandler:(failBlock)responError
{
    NSString *requestUrl = SERVER_URL(userUpDateUserInfo_Path);
    [[GMHTTPSessionManager shared] PUT:requestUrl parameters:paramDic success:^(NSURLSessionDataTask *task, id responseObject) {
        NSUInteger responseCode = [responseObject[@"code"] integerValue];
        //NSLog(@"responseObject :%@",responseObject);
        responSuccess(responseCode);
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}

// 更改头像
-(void)modifyUserHeader:(NSDictionary*)paramDic
                  image:(NSData*)imageData
         successHandler:(successBlock)responSuccess
           errorHandler:(failBlock)responError
{
    NSString *requestUrl = SERVER_URL(userModifyHeader_Path);
    [[GMHTTPSessionManager shared] POST:requestUrl parameters:paramDic constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileData:imageData name:@"image" fileName:@"avatar.jpg" mimeType:@"image/jpg"];
    } success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"responseObject");
        NSUInteger responseCode = [responseObject[@"code"] integerValue];
        if (responseCode == API_RESPONSE_OK) {
            PCUserModel *uModel = [[PCUserModel alloc] initWithDictionary:responseObject[@"data"]];
            self.userModel = uModel;
            // 存储新的用户信息
            [[PCUserInfoManager sharedUserInfo] saveUserInfo:uModel];
        }
        responSuccess(responseCode);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        //NSLog(@"responseObject");
        responError();
    }];
}

//个人相册列表
-(void)requestUserAlbumList:(NSDictionary*)paramDic
             successHandler:(successBlock)responSuccess
               errorHandler:(failBlock)responError
{
    NSString *requestUrl = SERVER_URL(userAlbum_Path);
    [[GMHTTPSessionManager shared] GET:requestUrl parameters:paramDic success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        
        NSUInteger responseCode = [responseObject[@"code"] integerValue];
        if (responseCode == API_RESPONSE_OK) {
            
            NSArray *dataArr = responseObject[@"data"];
            NSMutableArray *tmpArr = [NSMutableArray array];
            for (NSDictionary *aDic in dataArr) {
                PCUserAlbumModel *aModel = [[PCUserAlbumModel alloc] initWithDictionary:aDic];
                [tmpArr addObject:aModel];
            }
            responSuccess(responseCode,tmpArr);
        }else{
            responSuccess(responseCode,nil);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        responError();
    }];
}

// 相册加载更多
-(void)loadMoreAlbumList:(NSDictionary*)paramDic
          successHandler:(successBlock)responSuccess
            errorHandler:(failBlock)responError
{
    NSString *requestUrl = SERVER_URL(userAlbum_Path);
    [[GMHTTPSessionManager shared] GET:requestUrl parameters:paramDic success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        
//        NSUInteger responseCode = [responseObject[@"code"] integerValue];
//        responSuccess(responseCode);
        NSUInteger responseCode = [responseObject[@"code"] integerValue];
        if (responseCode == API_RESPONSE_OK) {
            
            NSArray *dataArr = responseObject[@"data"];
            NSMutableArray *tmpArr = [NSMutableArray array];
            for (NSDictionary *aDic in dataArr) {
                PCUserAlbumModel *aModel = [[PCUserAlbumModel alloc] initWithDictionary:aDic];
                [tmpArr addObject:aModel];
            }
            responSuccess(responseCode,tmpArr);
        }else{
            responSuccess(responseCode,nil);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

//个人赛事列表
-(void)requestUserGameList:(NSDictionary*)paramDic
            successHandler:(successBlock)responSuccess
              errorHandler:(failBlock)responError
{
    NSString *requestUrl = SERVER_URL(userGame_Path);
    [[GMHTTPSessionManager shared] GET:requestUrl parameters:paramDic success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
    
        NSUInteger responseCode = [responseObject[@"code"] integerValue];
        if (responseCode == API_RESPONSE_OK) {
            NSArray *dataArr = responseObject[@"data"];
            NSMutableArray *tmpArr = [NSMutableArray array];
            for (NSDictionary *aDic in dataArr) {
                PCUserGameModel *aModel = [[PCUserGameModel alloc] initWithDictionary:aDic];
                [tmpArr addObject:aModel];
            }
            responSuccess(responseCode,tmpArr);
        }else{
            responSuccess(responseCode,nil);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        responError();
    }];

}

-(void)loadMoreUserGameList:(NSDictionary*)paramDic
             successHandler:(successBlock)responSuccess
               errorHandler:(failBlock)responError
{
    NSString *requestUrl = SERVER_URL(userGame_Path);
    [[GMHTTPSessionManager shared] GET:requestUrl parameters:paramDic success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        
        NSUInteger responseCode = [responseObject[@"code"] integerValue];
        if (responseCode == API_RESPONSE_OK) {
            NSArray *dataArr = responseObject[@"data"];
            NSMutableArray *tmpArr = [NSMutableArray array];
            for (NSDictionary *aDic in dataArr) {
                PCUserGameModel *aModel = [[PCUserGameModel alloc] initWithDictionary:aDic];
                [tmpArr addObject:aModel];
            }
            responSuccess(responseCode,tmpArr);
        }else{
            responSuccess(responseCode,nil);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

-(void)requestUserFeedBack:(NSDictionary*)paramDic
            successHandler:(successBlock)responSuccess
              errorHandler:(failBlock)responError
{
    NSString *requestUrl = SERVER_URL(userFeedBakc_Back);
    [[GMHTTPSessionManager shared] POST:requestUrl parameters:paramDic success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSUInteger responseCode = [responseObject[@"code"] integerValue];
        responSuccess(responseCode);
        NSLog(@"responseObject :%@",responseObject);
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error :%@",error);
    }];

}

#pragma mark -
#pragma mark -支付
// 支付宝
-(void)requestAliPay:(NSDictionary*)paramDic
      successHandler:(successBlock)responSuccess
        errorHandler:(failBlock)responError
{
    NSString *requestUrl = SERVER_URL(aliPay_path);
    [[GMHTTPSessionManager shared] POST:requestUrl parameters:paramDic success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSLog(@"data :%@",responseObject[@"data"]);
        ;
        NSUInteger responseCode = [responseObject[@"code"] integerValue];
        if (responseCode == API_RESPONSE_OK) {
            responSuccess(responseCode,responseObject[@"data"]);
        }else{
            responSuccess(responseCode,responseObject[@"data"]);
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}

// 放弃参赛
-(void)giveUpGame:(NSDictionary*)paramDic
   successHandler:(successBlock)responSuccess
     errorHandler:(failBlock)responError
{
    NSString *requestUrl = SERVER_URL(userGiveUpGame_path);
    [[GMHTTPSessionManager shared] DELETE:requestUrl parameters:paramDic success:^(NSURLSessionDataTask *task, id responseObject) {

        NSUInteger responseCode = [responseObject[@"code"] integerValue];
        if (responseCode == API_RESPONSE_OK) {
            PCUserGameModel *aModel = [[PCUserGameModel alloc] initWithDictionary:responseObject[@"data"]];
            responSuccess(responseCode,aModel);
        }else{
            responSuccess(responseCode,nil);
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        responError();
    }];
}

// 上传原图
-(void)upLoadRealImage:(NSDictionary*)paramDic
                 image:(NSData*)imageData
        successHandler:(successBlock)responSuccess
          errorHandler:(failBlock)responError
{
    NSString *requestUrl = SERVER_URL(upLoadRealImage_Path);
    [[GMHTTPSessionManager shared] POST:requestUrl parameters:paramDic constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileData:imageData name:@"image" fileName:@"realImage.jpg" mimeType:@"image/jpg"];
    } success:^(NSURLSessionDataTask *task, id responseObject) {
        //NSLog(@"responseObject");
        NSUInteger responseCode = [responseObject[@"code"] integerValue];
        responSuccess(responseCode);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        responError();
    }];
}

//上传至相册
-(void)upLoadPhotoToAlbum:(NSDictionary*)paramDic
                 image:(NSData*)imageData
        successHandler:(successBlock)responSuccess
          errorHandler:(failBlock)responError
{
    NSString *requestUrl = SERVER_URL(upLoadPhotoToAlbum_Path);
    self.dataTask=[[GMHTTPSessionManager shared] POST:requestUrl parameters:paramDic constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        
        [formData appendPartWithFileData:imageData name:@"image" fileName:@"realImage.jpg" mimeType:@"image/jpg"];
    } success:^(NSURLSessionDataTask *task, id responseObject) {
        //NSLog(@"responseObject");
        NSUInteger responseCode = [responseObject[@"code"] integerValue];
        responSuccess(responseCode);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        responError();
    }];
}

//上传参赛
-(void)uploadPhotoForJoinMatch:(NSDictionary*)paramDic
                         image:(NSData*)imageData
                successHandler:(successBlock)responSuccess
                  errorHandler:(failBlock)responError{
    
    NSString *requestUrl = SERVER_URL(upLoadPhotoForJoinMatch_Path);
    [[GMHTTPSessionManager shared] POST:requestUrl parameters:paramDic constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        
        [formData appendPartWithFileData:imageData name:@"image" fileName:@"realImage.jpg" mimeType:@"image/jpg"];
    } success:^(NSURLSessionDataTask *task, id responseObject) {
        //NSLog(@"responseObject");
        NSUInteger responseCode = [responseObject[@"code"] integerValue];
        if (responseCode == API_RESPONSE_OK) {
            NSDictionary *data=responseObject[@"data"];
            NSString *uuid;
            if(data.count !=0){
                uuid=data[@"uuid"];
            }
            responSuccess(responseCode,uuid);

        }else{
            responSuccess(responseCode,nil);
 
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        responError();
    }];
}

@end
